home *** CD-ROM | disk | FTP | other *** search
/ LG Super CD / LG Super CD.iso / bitpim / bitpim-0.62-setup.exe / {app} / bitpim.exe / Scripts.py2exe / support.py < prev   
Encoding:
Text File  |  2003-11-06  |  8.3 KB  |  206 lines

  1. ##
  2. ##       Copyright (c) 2000, 2001 Thomas Heller
  3. ##
  4. ## Permission is hereby granted, free of charge, to any person obtaining
  5. ## a copy of this software and associated documentation files (the
  6. ## "Software"), to deal in the Software without restriction, including
  7. ## without limitation the rights to use, copy, modify, merge, publish,
  8. ## distribute, sublicense, and/or sell copies of the Software, and to
  9. ## permit persons to whom the Software is furnished to do so, subject to
  10. ## the following conditions:
  11. ##
  12. ## The above copyright notice and this permission notice shall be
  13. ## included in all copies or substantial portions of the Software.
  14. ##
  15. ## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. ## EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. ## MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. ## NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. ## LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. ## OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. ## WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. ##
  23.  
  24. import sys, imp
  25. sys.importers = []
  26.  
  27. for desc in imp.get_suffixes():
  28.     if desc[2] == imp.PY_COMPILED:
  29.         _pyc_suffix = desc
  30. del desc
  31.  
  32. # We install our special importers not in sys.path
  33. # but in sys.importers, so that sys.path only contain
  34. # strings.
  35. # So we have to sublcass imputil.ImportManager.
  36. #
  37. try:
  38.     unicode
  39. except NameError:
  40.     _stringtypes = (type(""),)
  41. else:
  42.     _stringtypes = (type(""), type(unicode("")))
  43.  
  44. class _MyImportManager(imputil.ImportManager):
  45.     def _import_top_module(self, name, _stringtypes=_stringtypes, sys=sys):
  46.         for item in sys.importers + sys.path:
  47.             for typ in _stringtypes:
  48.                 if isinstance(item, typ):
  49.                     module = self.fs_imp.import_from_dir(item, name)
  50.                     break
  51.             else:
  52.                 module = item.import_top(name)
  53.             if module:
  54.                 return module
  55.         return None
  56. del _stringtypes
  57.  
  58. _ModuleType = type(sys)         ### doesn't work in JPython...
  59.  
  60.  
  61. class _MyImporter(imputil.Importer):
  62.     # The following method is copied from imputil.Importer.
  63.     # There seems to be a bug in imputil somewhere which
  64.     # raises an 'AttributeError: __ispkg__' in the code
  65.     # marked below. So far I could not find this bug,
  66.     # so we work around it.
  67.     def _finish_import(self, top, parts, fromlist):
  68.         # if "a.b.c" was provided, then load the ".b.c" portion down from
  69.         # below the top-level module.
  70.         bottom = self._load_tail(top, parts)
  71.  
  72.         # if the form is "import a.b.c", then return "a"
  73.         if not fromlist:
  74.             # no fromlist: return the top of the import tree
  75.             return top
  76.  
  77.         # the top module was imported by self.
  78.         #
  79.         # this means that the bottom module was also imported by self (just
  80.         # now, or in the past and we fetched it from sys.modules).
  81.         #
  82.         # since we imported/handled the bottom module, this means that we can
  83.         # also handle its fromlist (and reliably use __ispkg__).
  84.  
  85.         # if the bottom node is a package, then (potentially) import some
  86.         # modules.
  87.         #
  88.         # note: if it is not a package, then "fromlist" refers to names in
  89.         #       the bottom module rather than modules.
  90.         # note: for a mix of names and modules in the fromlist, we will
  91.         #       import all modules and insert those into the namespace of
  92.         #       the package module. Python will pick up all fromlist names
  93.         #       from the bottom (package) module; some will be modules that
  94.         #       we imported and stored in the namespace, others are expected
  95.         #       to be present already.
  96.  
  97.         # The following line is the original one which raised the AttributeError:
  98.         #if bottom.__ispkg__:
  99.         # and this is the changed one:
  100.         if hasattr(bottom, '__ispkg__') and bottom.__ispkg__:
  101.             self._import_fromlist(bottom, fromlist)
  102.  
  103.         # if the form is "from a.b import c, d" then return "b"
  104.         return bottom
  105.  
  106.     def _process_result(self, (ispkg, code, values), fqname, imp=imp, sys=sys):
  107.         # did get_code() return an actual module? (rather than a code object)
  108.         is_module = isinstance(code, _ModuleType)
  109.  
  110.         # use the returned module, or create a new one to exec code into
  111.         if is_module:
  112.             module = code
  113.         else:
  114.             module = imp.new_module(fqname)
  115.  
  116.         ### record packages a bit differently??
  117.         module.__importer__ = self
  118.         module.__ispkg__ = ispkg
  119.  
  120.         # insert additional values into the module (before executing the code)
  121.         module.__dict__.update(values)
  122.  
  123.         # the module is almost ready... make it visible
  124.         sys.modules[fqname] = module
  125.  
  126.         # execute the code within the module's namespace
  127.         if not is_module:
  128.             exec code in module.__dict__
  129.  
  130.         # XXX (THe.) Here's the change from the original imputil:
  131.         return sys.modules[fqname]
  132.  
  133.     def get_code(self, parent, modname, fqname, get_code=get_code, _pyc_suffix=_pyc_suffix):
  134.         # Greg's importers return a dict containing the
  135.         # following items:
  136.         #
  137.         # __pkgdir__, __path__, __file__ for packages
  138.         # __file__ for normal modules
  139.     
  140.         # Usually 'parent', if not None, defines a context for
  141.         # importing. In the normal python import mechanism, parent.__path__
  142.         # is a list containing the package directory.
  143.         # Beware: There seem to be some unusual uses of this (See win32com.__init__)
  144.         # In Greg Steins importers module, parent.__path__ is the same as above,
  145.         # and parent.__ispkg__ is the package directory itself.
  146.         # If importing a package, a dict containing these items is returned as the
  147.         # third item and thus inserted into the new module.
  148.         # If importing a normal module, __file__ is inserted into the module.
  149.         # XXX What should WE do?
  150.  
  151.         import imp, marshal, sys
  152.  
  153.         dict = {}
  154.  
  155.         info = _extensions_mapping.get(fqname)
  156.         if info:
  157.             pathname, desc = info
  158.             # prepend exe_dir to filename
  159.             pathname = "%s\\%s" % (sys.prefix, pathname)
  160.             # Should catch IOError and convert into ImportError ??
  161.             fp = open(pathname, desc[1])
  162.             dict['__file__'] = pathname
  163.             return 0, imp.load_module(fqname, fp, pathname, desc), dict
  164.  
  165.         if hasattr(fqname, 'replace'):
  166.             fqname = fqname.replace('.', '\\')
  167.         else:
  168.             import strop
  169.             fqname = strop.replace(fqname, '.', '\\')
  170.             
  171.  
  172.         name = fqname + _pyc_suffix[0]
  173.         try:
  174.             code = get_code(name)
  175.         except KeyError:
  176.             pass
  177.         else:
  178.             dict['__file__'] = "<%s from archive>" % fqname
  179.             return 0, marshal.loads(code[8:]), dict
  180.  
  181.         name = fqname + '\\__init__' + _pyc_suffix[0]
  182.         try:
  183.             code = get_code(name)
  184.         except KeyError:
  185.             return None
  186.         else:
  187.             dict['__file__'] = "<package %s from archive>" % name
  188.             dict['__path__'] = [sys.path[0]]
  189.             return 1, marshal.loads(code[8:]), dict
  190.  
  191. _MyImportManager().install()
  192. sys.importers.append(imputil.BuiltinImporter())
  193. sys.importers.append(_MyImporter())
  194.  
  195. del _MyImportManager
  196. del _MyImporter
  197.  
  198. del _pyc_suffix
  199.  
  200. del get_code
  201. del sys, imp
  202. del imputil
  203.  
  204. # XXX We should not clobber the namespace this severe!!!
  205. _extensions_mapping = {'win32event': ('win32event.pyd', ('.pyd', 'rb', 3)), 'wxPython.calendarc': ('calendarc.pyd', ('.pyd', 'rb', 3)), '_sre': ('_sre.pyd', ('.pyd', 'rb', 3)), 'zlib': ('zlib.pyd', ('.pyd', 'rb', 3)), 'wxPython.helpc': ('helpc.pyd', ('.pyd', 'rb', 3)), 'win32file': ('win32file.pyd', ('.pyd', 'rb', 3)), '_winreg': ('_winreg.pyd', ('.pyd', 'rb', 3)), 'wxPython.htmlc': ('htmlc.pyd', ('.pyd', 'rb', 3)), 'wxPython.gizmosc': ('gizmosc.pyd', ('.pyd', 'rb', 3)), 'wxPython.gridc': ('gridc.pyd', ('.pyd', 'rb', 3)), 'datetime': ('datetime.pyd', ('.pyd', 'rb', 3)), 'wxPython.wxc': ('wxc.pyd', ('.pyd', 'rb', 3)), 'select': ('select.pyd', ('.pyd', 'rb', 3)), 'win32api': ('win32api.pyd', ('.pyd', 'rb', 3))}
  206.